btc(coin): won-block reassembly framing — assemble_won_block (reconstructor slice 4/7) - #838
Merged
Merged
Conversation
… (reconstructor slice 4/7)
Port the FRAMING half of p2pool data.py Share.as_block into
btc::coin::assemble_won_block: reconstruct the full block header from the
share's stored SmallBlockHeader + gentx txid walked up the (segwit-resolved)
merkle link via the sealed btc::check_merkle_link, frame [gentx] ++ other_txs
with the coinbase at index 0, and emit {bytes, hex} through the live BlockType
TX_WITH_WITNESS conditional codec (the NodeRPC::submit_block wire path).
BTC divergence from the DGB slice: BTC commits the BIP141 witness root inside
the coinbase at gentx-BUILD time (share_check.hpp:2166/:2296), so slices 1-3
hand this framer an already-committed gentx. It therefore does NOT re-inject a
witness commitment — doing so would mutate the coinbase, change its txid,
invalidate the share-committed header root, and open a second merkle path the
sealed merkle family (#570/#574/#579) forbids. The segwit-vs-legacy link
selection stays in the caller (slice 5); this framer takes a resolved MerkleLink.
Adds a fail-closed body/header guard: body_merkle_root folds [gentx_hash] ++ the
non-witness other-tx txids through the sealed btc::coin::compute_merkle_root and
assemble_won_block throws rather than emit a bad-txnmrklroot block. On a
template_capture miss this passes for empty-link (coinbase-only) shares and
correctly refuses the never-valid non-empty-link coinbase-only case.
Slice cut off origin/master. btc-tree-only. btc_share_test 82/82 (10 new
BtcBlockAssembly KATs, self-derived merkle branches).
Resolve btc test CMakeLists conflict by unioning the btc_share_test source list: keep both gentx_coinbase_test.cpp (slice 2/7, landed via #835) and block_assembly_test.cpp (slice 4/7, this branch).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Reconstructor slice 4/7. Ports the FRAMING half of p2pool
data.pyShare.as_blockintobtc::coin::assemble_won_block(src/impl/btc/coin/block_assembly.hpp):reconstruct_block_header— the share stores only aSmallBlockHeader(no merkle_root); recompute the header root asbtc::check_merkle_link(gentx_hash, merkle_link), exactly as verification did atcheck()time.assemble_won_block— frame[gentx] ++ other_txs(coinbase at index 0, template order) and emit{bytes, hex}through the liveBlockTypeTX_WITH_WITNESSconditional codec — the identicalNodeRPC::submit_blockwire path (rpc.cpp:pack<BlockType>(block)->submitblock).Reuse-only:
check_merkle_link,compute_merkle_root, and theBlockTypeserializer are all pre-existing sealed SSOTs. No new builders, no new merkle path.The no-re-inject rationale (please do not "fix" this back to the DGB shape)
BTC commits the BIP141 witness root inside the coinbase at gentx-BUILD time (
share_check.hpp:2166/:2296,generate_share_transaction), so slices 1-3 hand this framer an already witness-committed gentx. This framer therefore does NOT re-inject a witness commitment.Re-injecting a DGB-style
add_witness_commitmenthere would mutate the coinbase, change its txid, invalidate the share-committed header merkle_root, and open a second merkle path — which the sealed BTC merkle family (#570 / #574 / #579) forbids. The DGB lane adds its commitment downstream only because it does not commit at gentx-time; BTC does, so the correct BTC action is to reuse the committed gentx verbatim.The segwit-vs-legacy merkle-link selection (
segwit_data.txid_merkle_linkvsmerkle_link,share_check.hpp:674-692) stays in the caller (the reconstruct closure, slice 5): this framer takes an already-resolvedMerkleLink, exactly like the landed DGB slice — sealed link math reused, no new path introduced here.Fail-closed body/header self-check (compute_merkle_root)
Because BTC hands us the full template tx set,
assemble_won_blockverifies the assembled body against the share-committed header before emitting bytes:body_merkle_rootfolds[gentx_hash] ++ other-tx non-witness txidsthrough the sealedbtc::coin::compute_merkle_rootand the function throws on a mismatch rather than broadcast abad-txnmrklrootblock — consistent with the slice arc's "never emit a malformed block" posture (cf.gentx_unpack.hpp).Interaction with slice-3
template_capture.hpp(flagging for ratification): on a capture MISS slice 3 replays an emptytransactions[]. If the won share mined a coinbase-only template (empty link), body_root == header_root and the coinbase-only block broadcasts, carrying the full subsidy exactly astemplate_capturedocuments. If the share instead committed to fee txs (non-empty link), a coinbase-only body is abad-txnmrklrootblock the daemon would reject anyway; the guard fails closed so the closure does announce+audit instead of broadcasting a doomed block. Nothing broadcastable is lost — the guard only tightens the non-empty-link miss case, which was never valid.Test
btc_share_test82/82 green (10 newBtcBlockAssemblyKATs). Branches are self-derived from the SSOTother_tx_txid, so the body/header guard is exercised on genuinely consistent inputs; the throw path is tested explicitly (MismatchedBodyThrows).origin/master(not stacked on btc(#744): unpack_gentx_coinbase -- gentx bytes -> MutableTransaction codec #822/btc(#744): gentx_coinbase SSOT assembler -- reconstructor slice 2/7 #835/btc: reconstructor slice 3/7 — template_capture FIFO GBT-tx store (btc::coin::TemplateCapture) #837).block_assembly.hpp,block_assembly_test.cpp,test/CMakeLists.txt(registers the test in the existing allowlistedbtc_share_testtarget).Signed commit
3ca1ca9a5. I do not self-merge.